﻿using crypto;
using Microsoft.Ajax.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunShop_WebAPI.Model;

namespace YunShop_WebAPI.BLL
{
    /// <summary>
    /// UserID=当时电子会员卡时，UserID=MemberID
    /// OwnerID=当时电子会员卡时，OwnerID=WeixinID
    /// OwnerID=用户所属公司ID，换切门店后用到
    /// </summary>
    public enum SystemAppID
    {
        UserID = 0,
        CorpID = 1,
        OwnerID = 2,
        TopCorpID = 3,
        HostIPStr = 4,
        DbSplitID = 5,
        StockID = 6,
        WeixinID = 7,
        ModifyTime = 8,
        LoginDate = 9
    }

    public class SystemInfo
    {
        public static string sWebVersion = "V3.53.22.0618";//本程序版本号ID 

        public class usermodel
        {
            public string uname { get; set; }
            public string itemid { get; set; }
            public string systemid { get; set; }
        }

        public static void WriteInterfaceLog(string FSendStr, string FResultStr, string FDataType, string FResultFlag, string connstr)
        {
            try
            {
                string sql = string.Format(@"
                    Insert Into [dbo].[t_InterfaceLog]([FDateTime] ,[FDataType] ,[FSendStr] ,[FResultStr],[FResultFlag])
                    Values(getdate(),@FDataType,@FSendStr,@FResultStr,@FResultFlag)");
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@FDataType", SqlDbType.VarChar, 20, FDataType);
                par_list.Add("@FSendStr", SqlDbType.Text, 0, FSendStr.ToString());
                par_list.Add("@FResultStr", SqlDbType.Text, 0, FResultStr.ToString());
                par_list.Add("@FResultFlag", SqlDbType.VarChar, 2, FResultFlag);
                DBHelper.ExeSQL(sql, par_list.Get_List(), connstr);
            }
            catch
            {
            }
        }

        public static void writeServiceLog(string FSendStr, string FResultStr, string FDataType, string FResultFlag, string connstr)
        {
            try
            {
                string sql = string.Format(@"
                    Insert Into [dbo].[t_WebServiceLog]([FDateTime] ,[FDataType] ,[FSendStr] ,[FResultStr],[FResultFlag])
                    Values(getdate(),@FDataType,@FSendStr,@FResultStr,@FResultFlag)");
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@FDataType", SqlDbType.VarChar, 20, FDataType);
                par_list.Add("@FSendStr", SqlDbType.Text, 0, FSendStr.ToString());
                par_list.Add("@FResultStr", SqlDbType.Text, 0, FResultStr.ToString());
                par_list.Add("@FResultFlag", SqlDbType.VarChar, 2, FResultFlag);
                DBHelper.ExeSQL(sql, par_list.Get_List(), connstr);
            }
            catch
            {
            }
        }

        public static int GetSystemAppID(string dSystemAppJson, SystemAppID idType)
        {
            try
            {
                dSystemAppJson = dSystemAppJson.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(dSystemAppJson);
                int sSystemAppID = CommonFun.ConvertToInt(sDecAppID.Split(',')[(int)idType]);
                return sSystemAppID;
            }
            catch
            {
                return 0;
            }
        }

        public static long GetSystemAppBigID(string dSystemAppJson, SystemAppID idType)
        {
            try
            {
                dSystemAppJson = dSystemAppJson.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(dSystemAppJson);
                long sSystemAppID = CommonFun.ConvertToBigInt(sDecAppID.Split(',')[(int)idType]);
                return sSystemAppID;
            }
            catch
            {
                return 0;
            }
        }

        public static string GetSystemAppStr(string dSystemAppJson, SystemAppID idType)
        {
            try
            {
                dSystemAppJson = dSystemAppJson.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(dSystemAppJson);
                string sSystemAppID = CommonFun.ConvertToString(sDecAppID.Split(',')[(int)idType]);
                return sSystemAppID;
            }
            catch
            {
                return "";
            }
        }

        public static string GetSystemAppPsd(string dSystemAppJson)
        {
            try
            {
                dSystemAppJson = dSystemAppJson.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(dSystemAppJson);
                string sSystemAppID = CommonFun.ConvertToString(sDecAppID.Split(',')[2]);
                return sSystemAppID;
            }
            catch
            {
                return "";
            }
        }

        public static string GetClusterHostIP(string mSystemAppID)
        {
            string sDbHostIP = "";
            try
            {
                mSystemAppID = mSystemAppID.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(mSystemAppID);
                sDbHostIP = CommonFun.ConvertToString(sDecAppID.Split(',')[(int)SystemAppID.HostIPStr]);
            }
            catch
            {
            }
            if (string.IsNullOrEmpty(sDbHostIP))
            {
                int mCorpID = getSystemAppID(mSystemAppID, SystemAppID.CorpID);
                sDbHostIP = GetClusterHostIP(mCorpID);
            }
            return sDbHostIP;
        }

        public static string GetClusterConn(string mSystemAppID)
        {
            string sDbHostIP = ".";
            if (DBHelper.DebugFlag) sDbHostIP = "39.98.170.8";
            try
            {
                mSystemAppID = mSystemAppID.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(mSystemAppID);
                sDbHostIP = CommonFun.ConvertToString(sDecAppID.Split(',')[(int)SystemAppID.HostIPStr]);
                if (string.IsNullOrEmpty(sDbHostIP))
                {
                    int mCorpID = getSystemAppID(mSystemAppID, SystemAppID.CorpID);
                    return GetClusterConn(mCorpID);
                }
            }
            catch
            {
            }
            string sDbSplitID = SystemInfo.GetSystemAppStr(mSystemAppID, SystemAppID.DbSplitID);
            string connstr = string.Format(DBHelper.ConnCluster, sDbHostIP, sDbSplitID);
            return connstr;
        }

        public static int GetSplitCorpID(int mCorpID)
        {
            string connstr = DBHelper.ConnMaster;
            if (mCorpID >= 600000) connstr = DBHelper.ConnMasterSplit;
            string msql = string.Format(@"SELECT FParentID FROM dbo.t_Corpration with(NOLOCK) where FItemID = {0} and FParentID > 0", mCorpID);
            ArrayList dataCorp = DBHelper.Select(msql, connstr);
            if (dataCorp.Count > 0)
            {
                Hashtable record0 = (Hashtable)dataCorp[0];//如果有上级，分库标识和上级相同
                return CommonFun.ConvertToInt(record0["FParentID"]);
            }
            return mCorpID;
        }

        public static string GetClusterConn(int mCorpID)
        {
            string msql = string.Format(@"SELECT FDbHostIP,FDbSplit,FDbHostNetIP FROM t_CorpHost where FMinCorpID <= {0} and FMaxCorpID >= {0}", GetSplitCorpID(mCorpID));

            ArrayList dataAll = DBHelper.Select(msql, DBHelper.ConnMaster);
            if (dataAll.Count > 0)
            {
                Hashtable record = (Hashtable)dataAll[0];
                string sDbHostIP = CommonFun.ConvertToString(record["FDbHostIP"]);
                string sDbSplitID = CommonFun.ConvertToString(record["FDbSplit"]);
                if (DBHelper.DebugFlag) sDbHostIP = CommonFun.ConvertToString(record["FDbHostNetIP"]);
                string connstr = string.Format(DBHelper.ConnCluster, sDbHostIP, sDbSplitID);
                return connstr;
            }
            return DBHelper.ConnMaster;
        }

        public static string GetClusterHostIP(int mCorpID)
        {
            string sql = string.Format(@"SELECT FDbHostIP FROM t_CorpHost where FMinCorpID <= {0} and FMaxCorpID >= {0}", GetSplitCorpID(mCorpID));
            ArrayList dataAll = DBHelper.Select(sql, DBHelper.ConnMaster);
            if (dataAll.Count > 0)
            {
                Hashtable record = (Hashtable)dataAll[0];
                return CommonFun.ConvertToString(record["FDbHostIP"]);
            }
            return ".";
        }

        public static string GetClusterSplit(int mCorpID)
        {
            string sql = string.Format(@"SELECT FDbSplit FROM t_CorpHost where FMinCorpID <= {0} and FMaxCorpID >= {0}", GetSplitCorpID(mCorpID));
            ArrayList dataAll = DBHelper.Select(sql, DBHelper.ConnMaster);
            if (dataAll.Count > 0)
            {
                Hashtable record = (Hashtable)dataAll[0];
                return CommonFun.ConvertToString(record["FDbSplit"]);
            }
            return ".";
        }

        public static string GetClusterHostUrl(string shop_appid)
        {
            //int mMasterFlag = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.MasterFlag);

            //if (mMasterFlag == 1) return MiniMngCommon.YunShopUrl;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string sql = string.Format(@"SELECT FMainWebService FROM t_CorpHost where FMinCorpID <= {0} and FMaxCorpID >= {0}", mCorpID);

            ArrayList dataAll = DBHelper.Select(sql, DBHelper.ConnMaster);
            if (dataAll.Count > 0)
            {
                Hashtable record = (Hashtable)dataAll[0];
                return CommonFun.ConvertToString(record["FMainWebService"]);
            }
            else
            {
                return MiniMngCommon.YunShopUrl;
            }
        }

        public static int getSystemAppID(string dSystemAppJson, SystemAppID idType)
        {
            try
            {
                dSystemAppJson = dSystemAppJson.Replace(' ', '+');
                string sDecAppID = Security.Decrypt(dSystemAppJson);
                int sSystemAppID = CommonFun.ConvertToInt(sDecAppID.Split(',')[(int)idType]);
                return sSystemAppID;
            }
            catch
            {
                return 0;
            }
        }

        public static string SetSystemAppID(SystemAppModel systemApp)
        {
            try
            {
                //系统用户的身份
                string mSystemID = systemApp.FUserID.ToString() + "," + systemApp.FCorpID.ToString() + ","
                    + systemApp.FOwnerID.ToString() + "," + systemApp.FParentID.ToString() + ","
                    + systemApp.FHostIP + "," + systemApp.FDbSplit + "," + systemApp.FDtSplit
                    + systemApp.FStockID.ToString()+ "," + systemApp.FWeixinID.ToString() + ","
                    + systemApp.FModifyTime.ToString() + ","+ systemApp.FLoginDate.ToString();

                string sSystemAppID = Security.Encrypt(mSystemID);
                return sSystemAppID;
            }
            catch
            {
                return "";
            }
        }

        public static string SetSystemAppID(int iUserID,int iCorpID,int iStockID)
        {
            try
            {
                //系统用户的身份
                Hashtable result = new Hashtable();
                result["FHostID"] = 0;
                result["FIsMain"] = 0;
                result["FHostNumber"] = "";
                result["FUserID"] = iUserID;
                result["FCorpID"] = iCorpID;
                result["FStockID"] = iStockID;
                result["FTimeStamp"] = CommonFun.GetTimeStamp();
                string json = PluSoft.Utils.JSON.Encode(result);
                return System.Web.HttpUtility.UrlEncode(Security.Encrypt(json));
            }
            catch
            {
                return "";
            }
        }


    }


    public class GetItems
    {
        public long FItemID { get; set; }
        public string FNumber { get; set; }
        public string FName { get; set; }

    }
}